package com.nnnu.wsnackshop.controller.admin;


import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.dto.AddSpuDTO;
import com.nnnu.wsnackshop.pojo.dto.SpuPageDTO;
import com.nnnu.wsnackshop.pojo.dto.UpdateSpuDTO;
import com.nnnu.wsnackshop.pojo.vo.SpuVO;
import com.nnnu.wsnackshop.result.PageResult;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IProductSpuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 产品SPU表 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@RestController("adminProductSpuController")
@RequestMapping("/api/admin/spu")
@Tag(name = "管理端-SPU接口")
@Slf4j
@RequiredArgsConstructor
public class ProductSpuController {
    private final IProductSpuService spuService;

    @Operation(summary = "分页查询SPU")
    @GetMapping("/page")
    @RequiresRoles(value = {"superAdmin", "admin"}, logical = Logical.OR)
    @RequiresPermissions("spu:read")
    public Result<PageResult<SpuVO>> pageSpu(@ParameterObject SpuPageDTO dto) {
        return Result.success(spuService.pageSpu(dto), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取SPU详情")
    @GetMapping("/{id}")
    @RequiresPermissions("spu:read")
    public Result<SpuVO> getSpu(@PathVariable Integer id) {
        return Result.success(spuService.getSpuDetail(id), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "新增SPU")
    @PostMapping
    @RequiresPermissions("spu:create")
    public Result<Void> addSpu(@Valid @RequestBody AddSpuDTO dto) {
        spuService.addSpu(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    @Operation(summary = "更新SPU")
    @PutMapping
    @RequiresPermissions("spu:update")
    public Result<Void> updateSpu(@Valid @RequestBody UpdateSpuDTO dto) {
        spuService.updateSpu(dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "删除SPU")
    @DeleteMapping
    @RequiresPermissions("spu:delete")
    public Result<Void> deleteSpu(@RequestParam List<Integer> ids) {
        spuService.deleteSpu(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "导出SPU列表Excel")
    @GetMapping("/export")
    @RequiresPermissions("spu:export")
    public void exportSpu(HttpServletResponse resp) throws IOException {
        spuService.exportSpu(resp);
    }

    @Operation(summary = "下载SPU导入模板")
    @GetMapping("/template")
    @RequiresPermissions("spu:read")
    public void downloadSpuTemplate(HttpServletResponse resp) throws IOException {
        spuService.downloadSpuTemplate(resp);
    }

    @Operation(summary = "导入SPU Excel")
    @PostMapping("/import")
    @RequiresPermissions("spu:import")
    public Result<Void> importSpu(@RequestParam("file") MultipartFile file) throws IOException {
        spuService.importSpu(file);
        return Result.success(MessageConstants.ADD_OK);
    }
}
